<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" :close-on-press-escape="mode != 'show' ? false : true"
             destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px">
			<el-form-item label="客户" prop="client_id">
				<el-input v-model="client_name" placeholder="请选择客户" readonly @click="selectClient"></el-input>
			</el-form-item>
            <el-form-item label="联系人" prop="client_id">
                <el-select v-model="form.source" filterable style="width: 100%" placeholder="请选择联系人">
                    <el-option v-for="item in sourceList" :key="item.id" :label="item.source_name" :value="item.id" />
                </el-select>
			</el-form-item>
            <el-form-item label="跟单类型" prop="client_id">
                <el-select v-model="form.source" filterable style="width: 100%" placeholder="跟单类型">
                    <el-option label="电话" :value="1" />
                    <el-option label="网络" :value="2" />
                    <el-option label="上门" :value="3" />
                </el-select>
			</el-form-item>
            <el-form-item label="跟单内容" prop="remark">
                <el-input v-model="form.remark" placeholder="请输入跟单内容" clearable :rows="6"></el-input>
            </el-form-item>
            <el-form-item label="下次跟单时间" prop="project_time">
                <el-date-picker v-model="form.project_time" type="date" placeholder="下次跟单时间" style="width: 100%;" :value-format="'YYYY-MM-DD'"></el-date-picker>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
    <client-list v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></client-list>
</template>

<script>
import clientList from '../list/clientList.vue';
	export default {
        components: { clientList },
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id:"",
					source_name: "",
					// desc: "",
				},
				//验证规则
				rules: {
					source_name: [
						{required: true, message: '请输入来源名称'}
					]
				},
                dialog:{
                    save:false
                },
			}
		},
		mounted() {
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res  = [];
						if (this.form.id) {
							res = await this.$API.client.source.edit.post(this.form);
						}else{
							res = await this.$API.client.source.add.post(this.form);
						}
						this.isSaveing = false;
						if(res.code == 1){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.source_name = data.source_name
				// this.form.desc = data.desc
			},
            selectClient(){
                this.dialog.save = true
                this.$nextTick(() => {
                    this.$refs.saveDialog.open('add')
                })
            },
            handleSuccess(data){
                this.form.client_id = data.id
            },
		}
	}
</script>

<style>
</style>
